home *** CD-ROM | disk | FTP | other *** search
/ Mac OS 9 Serial Number Archive / SN Archive 2023.11.04.toast / BSNG / SDK / BSNG SDK 2.6 / Contributed / Basic / Template.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-22  |  7.1 KB  |  288 lines  |  [TEXT/CWIE]

  1. /*
  2.  * $Workfile:: Template.c                                                     $
  3.  * $Revision:: 3                                                              $
  4.  *
  5.  * $Author:: Buck Rogers                                                      $
  6.  * $Modtime:: 29.10.1997 17:52 Uhr                                            $
  7.  *
  8.  * $History:: Template.c                                                      $
  9.  * 
  10.  * *****************  Version 3  *****************
  11.  * User: Buck Rogers  Date: 29.10.1997   Time: 17:53 Uhr
  12.  * Updated in $/BSNG/Plugins/BSNG SDK/BSNG Template
  13.  * updated source to show how to make use of the new 'errorText' variable
  14.  * 
  15.  * *****************  Version 2  *****************
  16.  * User: Buck Rogers  Date: 08.10.1997   Time: 02:02 Uhr
  17.  * Updated in $/BSNG/Plugins/BSNG SDK/BSNG Template
  18.  * changed lf to cr in list generation so Mac compatible output is created
  19.  * 
  20.  * *****************  Version 1  *****************
  21.  * User: Buck Rogers  Date: 06.10.1997   Time: 06:25 Uhr
  22.  * Created in $/BSNG/Plugins/BSNG SDK/BSNG Template
  23.  * Adding subproject 'BSNG Template' to '$/BSNG/Plugins/BSNG SDK'
  24.  * 
  25.  * $NoKeywords::                                                              $
  26.  */
  27.  
  28.  
  29. #include <MixedMode.h>
  30. #include <A4Stuff.h>
  31.  
  32. #include "standard utils.h"
  33. #include "UltraU.h"
  34. #include "BSNG API.h"
  35. #include "BSNG Product.h"
  36. #include "Generate.h"
  37.  
  38. /*    don't touch this __procinfo definition or the calls from native code will crash the machine */
  39.  
  40. ProcInfoType __procinfo = kThinkCStackBased | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(BSNGParamBlockPtr)));
  41.  
  42. /* these should be moved into standard utils.h */
  43. OSErr WriteData(BSNGParamBlockPtr inData, const char* inText, long inLength);
  44. OSErr WriteCString(BSNGParamBlockPtr inData, const char* inString);
  45. OSErr WritePString(BSNGParamBlockPtr inData, StringPtr inString);
  46.  
  47.  
  48. /*    function prototypes for ANSI C */
  49.  
  50. static void DoInit(BSNGParamBlockPtr inData);
  51. static Boolean DoValidate(BSNGParamBlockPtr inData);
  52. static void DoCalc(BSNGParamBlockPtr inData);
  53. static void DoRandomCalc(BSNGParamBlockPtr inData);
  54. static Boolean DoAddRandomsToList(BSNGParamBlockPtr inData);
  55. static void DoItemHit(BSNGParamBlockPtr inData);
  56. static void DoCleanup(BSNGParamBlockPtr inData);
  57. void main(BSNGParamBlockPtr inData);
  58.  
  59.  
  60. /*    here is where the fun starts :-) */
  61.  
  62. static void DoInit(BSNGParamBlockPtr inData)
  63. {
  64.     /*    Initialize the Ultra Random Number generator (optional, use MacOS random functions otherwise) */
  65.     
  66.     Ultra_seed1 = inData->randSeed1;
  67.     Ultra_seed2 = inData->randSeed2;
  68.     Ultra_Init();
  69.     
  70.     /*    Give the BSNG App the informations if your plugin can do random calculations and if it can write
  71.         to the serial number list */
  72.     
  73.     inData->wantsRandomButton = true;
  74.     inData->canAddToSNList = true;
  75.     
  76.     /*  Fill out the default values; all values are initially empty. */
  77.     UserFillDefaults(inData);
  78.  
  79.     /*    Tell the BSNG App if the initialisation was successful (it was in our case) */
  80.     
  81.     inData->error = errExtNoErr;
  82.     inData->errorInItem = 0;
  83. }  /* DoInit */
  84.  
  85.  
  86. static Boolean DoValidate(BSNGParamBlockPtr inData)
  87. {
  88.     inData->errorText[0] = 0;
  89.     inData->error = errExtNoErr;
  90.     
  91.     if (UserValidate(inData) == false)
  92.     {
  93.         inData->error = errExtIncorrectValue;
  94.         return (false);
  95.     }
  96.     
  97.     return (true);
  98. }  /* DoValidate */
  99.  
  100.  
  101. static void DoCalc(BSNGParamBlockPtr inData)
  102. {
  103.     StringPtr       result;
  104.     short            resultIndex;
  105.     
  106.     /* do your calculations here */
  107.     resultIndex = UserCalculate(inData);
  108.     if (resultIndex >= kItemValue1 && resultIndex <= kItemValue10)
  109.     {
  110.         result = inData->itemValue[resultIndex];
  111.     
  112.         ZeroScrap();
  113.         PutScrap(result[0], 'TEXT', result + 1);
  114.     }
  115.  
  116. }  /* DoCalc */
  117.  
  118.  
  119. static void DoRandomCalc(BSNGParamBlockPtr inData)
  120. {
  121.     /*    prepare random calculations here */
  122.     UserRandomize(inData);
  123.     
  124.     DoCalc(inData);
  125. }  /* DoRandomCalc */
  126.  
  127.  
  128. static const char gEquals[] = "================================"
  129.                               "================================"
  130.                               "================================"
  131.                               "================================";
  132.  
  133. static Boolean DoAddRandomsToList(BSNGParamBlockPtr inData)
  134. {
  135.     long            count = 0L;
  136.     short            i = 0;
  137.     OSErr            err = noErr;
  138.     
  139.     if (WriteCString(inData, PROGRAM_NAME) != noErr)
  140.         goto failed;
  141.  
  142.     if (WriteCString(inData, "\r") != noErr)
  143.         goto failed;
  144.     
  145.     if (WriteData(inData, gEquals, cStrLen(PROGRAM_NAME, 0x7FFFFFFF)) != noErr)
  146.         goto failed;
  147.     
  148.     if (WriteCString(inData, "\r") != noErr)
  149.         goto failed;
  150.     
  151.     /*    write your header to the number list */
  152.     
  153.     if (UserWriteListHeader(inData) != noErr)
  154.         goto failed;
  155.     
  156.     /*    create numOfListNumbers random serial numbers and write them to the list */
  157.     /*    if you create a name-based generator please make sure that you create at least one number */
  158.     /*    using the name, company and/or numCopies informations. You could create more name-based numbers */
  159.     /*    by using your own name/company database */
  160.     
  161.     for (i = 0; i < inData->numOfListNumbers; i++)
  162.     {
  163.         DoRandomCalc(inData);
  164.         
  165.         /* write results to list here */
  166.         if (UserWriteListNumber(inData) != noErr)
  167.             goto failed;
  168.     }
  169.     
  170.     /* don't forget this last newline */
  171.  
  172.     if (WriteCString(inData, "\r") != noErr)
  173.         goto failed;
  174.     
  175.     return (true);
  176.  
  177. failed:
  178.     return false;
  179.     
  180. }  /* DoAddRandomsToList */
  181.  
  182.  
  183. static void DoItemHit(BSNGParamBlockPtr inData)
  184. {
  185.     switch (inData->itemMessage)
  186.     {
  187.         case (300000):
  188.         
  189.             /*    the "About this plugin" button was pressed, do whatever you want, I just display a small Alert here */
  190.         
  191.             NoteAlert(1000, nil);
  192.             break;
  193.         
  194.         default:
  195.             break;
  196.     }
  197. }  /* DoItemHit */
  198.  
  199.  
  200. static void DoCleanup(BSNGParamBlockPtr inData)
  201. {
  202. #pragma unused (inData)
  203.  
  204.     /*    we didn't allocate any memory in DoInit, so we can leave this empty, otherwise it would be a VERY good idea
  205.         to deallocate/dispose etc. everything we allocated during our work. If you don't do that we have a nice memory leak */
  206.     
  207. }  /* DoCleanup */
  208.  
  209.  
  210. void main(BSNGParamBlockPtr inData)
  211. {
  212. #if (!GENERATINGPOWERPC)
  213.     EnterCodeResource();
  214. #endif
  215.     
  216.     /*    the message dispatcher */
  217.     
  218.     switch(inData->theMessage)
  219.     {
  220.         case (msgExtInit):
  221.             DoInit(inData);
  222.             break;
  223.         
  224.         case (msgExtCalcHit):
  225.         
  226.             if (DoValidate(inData))
  227.             {
  228.                 DoCalc(inData);
  229.             }
  230.             
  231.             break;
  232.         
  233.         case (msgExtRandomHit):
  234.             DoRandomCalc(inData);
  235.             break;
  236.         
  237.         case (msgExtCreateRandom):
  238.             
  239.             /*    report to the BSNG App if your list entry was written ok or if we had an error */
  240.             
  241.             if (DoAddRandomsToList(inData))
  242.             {
  243.                 inData->error = errExtNoErr;
  244.             }
  245.             else
  246.             {
  247.                 inData->error = errExtWritingToList;
  248.             }
  249.             
  250.             break;
  251.         
  252.         case (msgExtItemHit):
  253.             DoItemHit(inData);
  254.             break;
  255.         
  256.         case (msgExtCleanup):
  257.             DoCleanup(inData);
  258.             break;
  259.         
  260.         default:
  261.             break;
  262.     }
  263.     
  264. #if (!GENERATINGPOWERPC)
  265.     ExitCodeResource();
  266. #endif
  267. }  /* main */
  268.  
  269.  
  270. /* As mentioned at the top of the file, these should be moved to standard utils.c */
  271. OSErr WriteData(BSNGParamBlockPtr inData, const char* inText, long inLength)
  272. {
  273.     long count = inLength;
  274.     
  275.     return FSWrite(inData->outputRefNum, &count, inText);
  276. }
  277.  
  278. OSErr WriteCString(BSNGParamBlockPtr inData, const char* inString)
  279. {
  280.     return WriteData(inData, inString, cStrLen((const Ptr) inString, 0x7FFFFFFF));
  281. }
  282.  
  283. OSErr WritePString(BSNGParamBlockPtr inData, StringPtr inString)
  284. {
  285.     return WriteData(inData, (const char*) (inString + 1), inString[0]);
  286. }
  287.  
  288.